<?php
class Remessa{
	private $id=0;
	private $valorEntregue=0;
	private $referenciaMultibanco;

	public function getValorEntregue()
	{
		return $this->valorEntregue;
	}
	public function setValorEntregue($valor)
	{
		$this->valorEntregue=$valor;
	}
	public function cancelar()
	{}
	public function editar()
	{}
	public function enviar()
	{}
	public function validar()
	{}
	public function bloquear()
	{}
	public function desbloquear()
	{}
	public function imprimir()
	{}
	private function setReferenciaMultibanco($referencia){
		$this->referenciaMultibanco=$referencia;
	}
	public function getReferenciaMultibanco(){
		return $this->referenciaMultibanco;
	}
	private function geraReferenciaMultiBanco(){
		$result=false;
		$referencia = Referencia::geraReferencia($this->id, $this->getValorEntregue());
		if(!is_numeric($referencia)){
			echo 'erro na geracao da remessa<br />';
			return $result;
		}
		$result=true;
		$this->setReferenciaMultibanco($referencia);
		return $result;
	}
	private function insereReferenciaMultibanco()
	{
		global $db;
		if (isset($_POST['referencia']) && $_POST['referencia'] == 'on') {
			$result=false;
			//gera a referencia
			$this->geraReferenciaMultiBanco();
			$sql="UPDATE facturasextjoins SET referencia ='".$this->getReferenciaMultibanco()."' WHERE FraExtID = {$this->getId()}";
			//echo $sql;
			$result=$db->query($sql);
			if(!$result){
				echo 'erro na inserção da remessa<br />';
				echo $sql.'<br />';
			}
			return $result;
		}
		else{
			// Não foi selecionada a opção de referencia de multibanco
			// Devolver true
			return true;
		}
	}
	/*
	 *FraFechaIngreso,
	 FraCorresponsal,
	 FraAgenciaPago,
	 FraReferenciaAg,
	 FraRefAux,
	 FraReferenciaCo,
	 FraComisionAg,
	 FraComisionAgR,
	 FraRetencion,
	 FraComisionComercial,
	 FraComisionCorr,
	 FraOrigen0,
	 FraOrigen1,
	 FraOrigen2,
	 FraOrigen3,
	 FraCliNumero,
	 FraBnfNumero,
	 FraCtaBenef,
	 FraMonCobro,
	 FraEnviado,
	 FraCargo,
	 FraEnviadoUSD,
	 FraCargoUSD,
	 FraUrgente,
	 FraMonDPago,
	 FraARecibir,
	 FraDolares,
	 FraCambio,
	 FraTasaAgn,
	 FraTasa,
	 FraMensaje,
	 FraNotas,
	 FraClave,
	 FraConcepto,
	 FraDifCambio,
	 FraStatus,
	 FraModoPago,
	 FraProm,
	 FraTaxProm

	 in va7 datetime,
	 in va10 int,
	 in va11 int,
	 in va13 int,
	 in va14 int,
	 in va17 decimal(19,4),
	 in va18 decimal(19,4),
	 in va19 decimal(19,4),
	 in va20 decimal(19,4),
	 in va21 decimal(19,4),
	 in va22 mediumint(8),
	 in va23 mediumint(8),
	 in va24 varchar(15),
	 in va25 varchar(20),
	 in va26 int,
	 in va27 int,
	 in va28 int,
	 in va29 smallint(5),
	 in va30 decimal(19,4),
	 in va31 decimal(19,4),
	 in va32 decimal(19,4),
	 in va33 decimal(19,4),
	 in va34 tinyint(3),
	 in va35 varchar(1),
	 in va36 decimal(19,4),
	 in va37 decimal(19,4),
	 in va38 decimal(19,4),
	 in va39 decimal(19,4),
	 in va40 float,
	 in va41 varchar(100),
	 in va42 varchar(100),
	 in va43 varchar(15),
	 in va44 int,
	 in va45 decimal(19,4),
	 in va46 int,
	 in va47 int,
	 in va48 decimal(19,2),
	 in va49 decimal(19,2),
	 */
	/*
	 * '2011-03-22 12:02:52',
	 * 301,
	 * 0,
	 * 0,
	 * 0,
	 * 0.00,
	 * 0,
	 * 0,
	 * 0,
	 * 0.00,
	 * 811,
	 * 811,
	 * '601',
	 * '127.0.0.1',
	 * 1,
	 * 1,
	 * 1,
	 * 1,
	 * 500.00,
	 * 0.00,0,0,0,3,1075.00,0,0,2.1500,2.1500,'','',0,1,0,2,1,0,0,@FraID
	 *
	 * */

	public function __construct($data,$correspondente,$Fi11,$Fi13,$Fi14,$Fi17,$Fi18,$Fi19,$Fi20,$Fi21,$Fi22,$Fi23,$Fi24,$Fi25,$idCliente,$idBeneficiario,$Fi28,$Fi29,$Fi30,$Fi31,$Fi32,$Fi33,$Fi34,$Fi35,$Fi36,$Fi37,$Fi38,$Fi39,$Fi40,$Fi41,$Fi42,$Fi43,$Fi44,$Fi45,$Fi46,$Fi47,$Fi48,$Fi49) {
			
		$sql="CALL do_invoice ('{$data}',{$correspondente},{$Fi11},{$Fi13},{$Fi14},{$Fi17},{$Fi18},{$Fi19},{$Fi20},{$Fi21},{$Fi22},{$Fi23},'{$Fi24}','{$Fi25}',{$idCliente},{$idBeneficiario},{$Fi28},{$Fi29},{$Fi30},{$Fi31},{$Fi32},{$Fi33},{$Fi34},{$Fi35},{$Fi36},{$Fi37},{$Fi38},{$Fi39},{$Fi40},'{$Fi41}','{$Fi42}',{$Fi43},{$Fi44},{$Fi45},{$Fi46},{$Fi47},{$Fi48},{$Fi49},@FraID); SELECT @FraID;";
		$this->setValorEntregue($Fi29);
		$result = tuga_multiquery($sql);
		$this->setId($result[0][0]);
		if(!is_numeric($this->getId()))
		{
			echo 'erro na inserção da remessa<br />';
			echo $sql.'<br />';
			return false;
		}
		$result=$this->insereReferenciaMultibanco();
		return $result;
	}
	public function getId(){
		return $this->id;
	}
	public function setId($id){
		$this->id=$id;
	}
}